<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/19  14:05
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Think\Controller;
//用户中心
Class UserController extends BaseController{

    function index(){
        $User=M("User");
        $user_id=session("user_id");
        $d=$User->where("user_id=$user_id")->field("headimgurl,nickname")->find();
        $this->assign("d",$d);
        $this->display();
    }

    //个人信息
    function info(){
        $user_id=session("user_id");
        $User=M("User");
        $d=$User->where("user_id=$user_id")->field("user_id,nickname,sex,headimgurl")->find();
        $this->assign("d",$d);
        $this->display();
    }

    //我的收藏-默认收藏图书
    function collect(){
        $this->redirect("collectbook");
    }

    //我的收藏-图书
    function collectbook(){
        $fav_book=M("favorite_book");
        $user_id=session("user_id");
        $d=$fav_book->alias("a")->where("user_id=$user_id")->join("book b on a.book_id=b.book_id")->field("a.book_id,b.sm,b.b_logo,b.dj")->select();

        $this->assign("d",$d);
        $this->display();
    }

    //我的收藏-活动
    function collectact(){
        $fav_book=M("favorite_activity");
        $user_id=session("user_id");
        $d=$fav_book->alias("a")->where("user_id=$user_id")->join("activity b on a.activity_id=b.activity_id")->field("a.activity_id,b.a_logo")->select();
        $this->assign("d",$d);
        $this->display();
    }

    //参与活动-已参加
    function activity_join(){
        $vactivity=M("View_activity_order");
        $user_id=session("user.user_id");
        //已支付且未退款
        $d=$vactivity->where(['user_id'=>$user_id,'status'=>1,'is_refund'=>'0'])->select();
        $this->assign("d",$d);
        $this->display();
    }

    //参与活动-未参加
    function activity_notjoin(){
        $vactivity=M("View_activity_order");
        $user_id=session("user_id");
        //未支付或已退款的
        $d=$vactivity->where(['user_id'=>$user_id,'_complex'=>['status'=>0,'is_refund'=>'1','_logic'=>'or']])->select();
        $this->assign("d",$d);
        $this->display();
    }

    //借阅订单-全部
    function order_rent(){
        $user_id=session("user_id");
        $Orderbook=D("OrderBook");
        $Pay=M("Pay");
        $mapp['status']='-1';
        $dp=$Pay->where($mapp)->field("pay_id")->select();
        $cancelp=array();
        foreach($dp as $k=>$v){
            array_push($cancelp,$v['pay_id']);
        }
        $map["user_id"]=$user_id;
        $map["type"]=1;
        if(sizeof($cancelp)>0){
            $map["pay_id"]=array("not in",$cancelp);
        }
        $d=$Orderbook->where($map)->relation(true)->order("create_time desc")->select();
        foreach($d as $k=>$v){
            $total=0;
            $is_apprised=0;//未评价的条数,
            $is_back=0;//未归还的图书数量
            foreach($d[$k]['order_has_book'] as $kk=>$vv){
                //计算商品数
                $total+=$vv['quantity'];
                if($d[$k]['order_has_book'][$kk]['is_apprised']==0){
                    $is_apprised++;
                }
                if($d[$k]['order_has_book'][$kk]['is_backed']==0){
                    $is_back++;
                }
            }
            $d[$k]['total_rent']=$total;
            //收货后是否评价和是否归还
            if($is_apprised > 0 && $d[$k]['state_order']==3 && $is_back>0){
                $d[$k]['state_order']=5;//已发货未评价未归还
            }
            if($is_apprised==0 && $d[$k]['state_order']==3 && $is_back>0){
                $d[$k]['state_order']=6;//已发货已评价未归还
            }

            if($is_apprised>0 && $d[$k]['state_order']==3 && $is_back==0){
                $d[$k]['state_order']=7;//已发货未评价已归还
            }
            if($is_apprised==0 && $d[$k]['state_order']==3 && $is_back==0){
                $d[$k]['state_order']=8;//已发货已评价已归还
            }
        }
        $this->assign("d",$d);
        $this->display();
    }

    //借阅订单-详情
    function rent_detail(){
        $user_id=session("user_id");
        $order_id=I("get.id");
        $Orderbook=D("OrderBook");
        $d=$Orderbook->where("user_id=$user_id AND order_id=$order_id AND type=1")->relation(true)->find();
        $total=0;
        foreach($d['order_has_book'] as $k=>$v){
            $total+=$d['order_has_book'][$k]['quantity'];
        }
        $d['total_rent']=$total;
        $this->assign("d",$d);
        $this->display();
    }

    //购书订单-全部
    function order_buy(){
        $user_id=session("user_id");
        $Orderbook=D("OrderBook");
        $Pay=M("Pay");
        $dp=$Pay->where("status=-1")->field("pay_id")->select();
        $cancelp=array();
        foreach($dp as $k=>$v){
            array_push($cancelp,$v['pay_id']);
        }
        $map["user_id"]=$user_id;
        $map["type"]=0;
        if(sizeof($cancelp)>0){
            $map["pay_id"]=array("not in",$cancelp);
        }

        $d=$Orderbook->relation(true)->where($map)->order("create_time desc")->select();
        foreach($d as $k=>$v){
            $total=0;
            $is_apprised=0;//未评价的条数,
            foreach($d[$k]['order_has_book'] as $kk=>$vv){
                //计算商品数
                $total+=$vv['quantity'];
                if($d[$k]['order_has_book'][$kk]['is_apprised']==0){
                    $is_apprised++;
                }
            }
            $d[$k]['total_rent']=$total;
            if($is_apprised==0){
                $d[$k]['state_order']=4;
            }
            if($v['is_refund']==1){
                $d[$k]['state_order']=5;
            }
        }
        $this->assign("d",$d);
        $this->display();
    }

    //购书订单-详情
    function buy_detail(){
        $user_id=session("user_id");
        $order_id=I("get.id");
        $Orderbook=D("OrderBook");
        $d=$Orderbook->where("user_id=$user_id AND order_id=$order_id AND type=0")->relation(true)->find();
        $total=0;
        foreach($d['order_has_book'] as $k=>$v){
            $total+=$d['order_has_book'][$k]['quantity'];
        }
        $d['total_rent']=$total;
        $Order=M("Order_book");
        $do=$Order->where("user_id=$user_id AND order_id=$order_id AND type=0")->field("discount_buy")->find();
        $d['discount_buy']=$do['discount_buy'];
        $this->assign("d",$d);
        $this->display();
    }

    //地址
    function address(){
        $user_id=session("user_id");
        $Adr=M("Address");
        $d=$Adr->where("user_id=$user_id AND is_default=1")->find();
        $this->assign("d",$d);
        $this->display();
    }

    //保存地址-ajax调用，1：成功，0：失败
    function saveaddress(){
        $map['user_id']=session("user_id");
        $map['is_default']=1;

        $data['name']=I("get.name");
        $data['phone']=I("get.phone");
        $city=explode(" ",I("get.city"));
        $data['province']=$city[0];
        $data['city']=$city[1];
        $data['district']=$city[2];
        $data['address']=I("get.address");
        $Adr=M("Address");
        $count=$Adr->where($map)->count();
        if($count==1){
            $Adr->where($map)->data($data)->save();
            echo 1;
            return;
        }
        $data['user_id']=session("user_id");
        $data['is_default']=1;
        $count=$Adr->data($data)->add();
        if($count){
            echo 1;
        }else{
            echo 0;
        }
    }

    /*
     * 生成借阅信息，即待生成订单信息，用户确认
     * id,num-id,num,....
     * 以-分隔条目，以,分隔内容
     * */
    function bag_order_confirm(){
        $liststr=I("get.liststr");
        $list=bookinfo_strtoarr($liststr);
        $book_id_arr=array();
        $total=0;
        foreach($list as $k=>$v){
            array_push($book_id_arr,$v['book_id']);
            $total+=$v['num'];
        }
        //想借阅的图书列表
        $map["book_id"]=array("in",$book_id_arr);
        $Book=M("Book");
        $d=$Book->where($map)->field("sm,zyz,b_logo")->order("book_id")->select();
        foreach($d as $k=>$v){
            foreach($list as $kk=>$vv){
                if($d[$k]['book_id']==$v['num']){
                    $d[$k]['rent_num']=$list[$k]['num'];
                }
            }
        }
        //收货地址
        $Adr=M("Address");
        $user_id=session("user_id");
        $map["user_id"]=$user_id;
        $map["is_default"]=1;
        $add=$Adr->where($map)->find();
        //可免借阅运费次数
        $Card=M("Card");
        $delivery=$Card->where("user_id=$user_id")->field("num_delivery")->find();
        $num_delivery=$delivery['num_delivery'];

        //借阅运费
        $User=D("User");
        $price_rent=$User->getFreightRent();

        $this->assign("price_rent",$price_rent);
        $this->assign("num_delivery",$num_delivery);
        $this->assign("add",$add);
        $this->assign("total",$total);
        $this->assign("d",$d);
        $this->display();
    }
    //活动报名页面
    function activity_order_confirm(){
        $activity_id=I("get.id");
        $user_id=session("user_id");
        //活动详情
        $Activity=M("Activity");
        $field="activity_id,title,a_logo,price_apply,price_material,apply_start,apply_end,apply_num,num_deduct";
        $d=$Activity->where("activity_id=$activity_id")->field($field)->find();
        //活动费用的计算
        $User=D("User");
        $frees=$User->getFreenum_apply(); //可免报名费次数
        var_dump($frees);
        if($frees>=$d['num_deduct']){
            $d['num_deduct_use']=$d['num_deduct'];//消耗的报名费次数
            $d['price_apply_discount']=0; //免活动费
        }else{
            $d['num_deduct_use']=0;//消耗的报名费次数
            $discount=$User->getDiscount_apply();
            $d['price_apply_discount']=$d['price_apply']*$discount; //活动折扣价
        }
        $d["price_total"]=$d["price_apply_discount"]+$d["price_material"];  //总费用（打折后的实际总金额）=打折后活动费+材料费
        //收货地址
        $Adr=M("Address");
        $map["user_id"]=$user_id;
        $map["is_default"]=1;
        $add=$Adr->where($map)->find();

        $this->assign("add",$add);
        $this->assign("d",$d);
        $this->display();
    }

    //购物车确认页面
    function cart_order_confirm(){
        $liststr=I("get.liststr");
        $list=bookinfo_strtoarr($liststr);
        //var_dump($list);
        $book_id_arr=array();
        $total=0;
        foreach($list as $k=>$v){
            array_push($book_id_arr,$v['book_id']);
            $total+=$v['num'];
        }
        $user_id=session("user_id");
        //购书折扣
        $Card=M("Card");
        $delivery=$Card->where("user_id=$user_id")->field("discount_buy")->find();
        $discount_buy=$delivery['discount_buy'];

        //想购买的图书列表
        $map["book_id"]=array("in",$book_id_arr);
        $Book=M("Book");
        $d=$Book->where($map)->field("sm,zyz,b_logo,dj")->order("book_id")->select();
        $price_total=0;
        $price_buy=0;
        foreach($d as $k=>$v){
            foreach($list as $kk=>$vv){
                if($d[$k]['book_id']==$v['num']){
                    $d[$k]['rent_num']=$list[$k]['num'];
                }
            }
            //会员价
            $d[$k]['dj_vip']=$d[$k]['dj']*$discount_buy;
            $price_buy+=$d[$k]['dj_vip']*$d[$k]['rent_num'];
        }
        //收货地址
        $Adr=M("Address");
        $map["user_id"]=$user_id;
        $map["is_default"]=1;
        $add=$Adr->where($map)->find();

        //购书运费
        $User=D("User");
        $freight_buy=$User->getFreightBuy();

        $price_total=$freight_buy+$price_buy;

        $num_delivery = D('card')->where("user_id=$user_id")->getField('num_delivery');

        $this->assign("freight_buy",$freight_buy);
        $this->assign("num_delivery",$num_delivery);
        $this->assign("add",$add);
        $this->assign("price_total",$price_total);
        $this->assign("total",$total);
        $this->assign("d",$d);
        $this->display();
    }
    //充值页面
    function vip_charge(){
        $user_id=session("user_id");
        //会员头像及到期时间
        $Card=M("Card");
        $dc=$Card->where("user_id=$user_id")->field("expire_time")->find();
        $expire_time=$dc['expire_time'];

        $User=M("User");
        $du=$User->where("user_id=$user_id")->field("headimgurl")->find();
        $headimgurl=$du['headimgurl'];
        //充值类型
        $type=M("Deposit_type");
        $dt=$type->where(['wx_show'=>'1'])->order("month")->select();
        $this->assign("expire_time",$expire_time);
        $this->assign("headimgurl",$headimgurl);
        $this->assign("dt",$dt);
        $this->display();
    }
}